package com.quectel.core.module.resource.service.impl;


import com.quectel.constant.global.SystemConstants;
import com.quectel.core.constants.RedisCacheConstants;
import com.quectel.core.module.resource.dao.ResourceGroupRelationDao;
import com.quectel.core.module.resource.dto.ResourceGroupRelationDto;
import com.quectel.core.module.resource.entity.ResourceGroupRelationEntity;
import com.quectel.core.module.resource.service.ResourceGroupRelationService;
import com.quectel.util.common.CopyUtils;
import com.quectel.util.kit.CacheKit;
import com.quectel.util.kit.Snowflake;
import com.quectel.util.mybatis.MyBatisPlusUtils;
import org.apache.dubbo.config.annotation.DubboService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

/**
 * @author rananxin
 * @date 2021-12-08 16:12:22
 */
@DubboService
public class ResourceGroupRelationServiceImpl implements ResourceGroupRelationService {

    private static final Logger LOGGER = LoggerFactory.getLogger(ResourceGroupRelationServiceImpl.class);

    private static final String RESOURCE_GROUP_RELATION_CACHE_BY_ID = RedisCacheConstants.PROJECT_NAME + "RESOURCE_GROUP_RELATION_CACHE_BY_ID:";

    @Autowired
    private ResourceGroupRelationDao resourceGroupRelationDao;


    @Override
    public ResourceGroupRelationDto selectCacheById(Long id) {
        return CacheKit.cacheToRedis(
                () -> selectById(id),
                RESOURCE_GROUP_RELATION_CACHE_BY_ID + id,

                SystemConstants.NOT_NULL_CACHE_EXPIRE_SECONDS
        );
    }

    @Override
    public ResourceGroupRelationDto selectById(Long id) {
        ResourceGroupRelationEntity entity = resourceGroupRelationDao.selectById(id);
        return paddingField(CopyUtils.copyObj(entity, ResourceGroupRelationDto.class));
    }

    @Override
    public List<ResourceGroupRelationDto> queryList(Map<String, Object> params) {
        List<ResourceGroupRelationEntity> list = resourceGroupRelationDao.queryList(params);
        List<ResourceGroupRelationDto> result = CopyUtils.copyList(list, ResourceGroupRelationDto.class);
        result.forEach(this::paddingField);
        return result;
    }

    private ResourceGroupRelationDto paddingField(ResourceGroupRelationDto dto) {
        if (dto != null) {

        }
        return dto;
    }

    @Override
    public int queryTotal(Map<String, Object> params) {
        return resourceGroupRelationDao.queryTotal(params);
    }

    @Override
    public Long save(ResourceGroupRelationDto dto) {
        ResourceGroupRelationEntity entity = CopyUtils.copyObj(dto, ResourceGroupRelationEntity.class);
        entity.setId(Snowflake.nextId());
        resourceGroupRelationDao.insert(entity);
        return entity.getId();
    }

    @Override
    public void updateById(ResourceGroupRelationDto dto) {
        ResourceGroupRelationEntity entity = CopyUtils.copyObj(dto, ResourceGroupRelationEntity.class);
        resourceGroupRelationDao.updateById(entity);
        CacheKit.invalidRedisCache(RESOURCE_GROUP_RELATION_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void updateAllColumnById(ResourceGroupRelationDto dto) {
        ResourceGroupRelationEntity entity = CopyUtils.copyObj(dto, ResourceGroupRelationEntity.class);


        MyBatisPlusUtils.updateAllColumnById(entity, resourceGroupRelationDao);

        CacheKit.invalidRedisCache(RESOURCE_GROUP_RELATION_CACHE_BY_ID + dto.getId());
    }

    @Override
    public void deleteById(Long id) {
        resourceGroupRelationDao.deleteById(id);

        CacheKit.invalidRedisCache(RESOURCE_GROUP_RELATION_CACHE_BY_ID + id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteBatch(Long[] ids) {
        for (Long id : ids) {
            deleteById(id);
        }
    }
}
